<template>
  <div class="home_page">
    <home-header-nav @goodsPageCall="fillGoodsPage"></home-header-nav>
    <div class="page_body">
      <thumbnail :list="avaList" @select="gotoPreview"></thumbnail>
    </div>
    <div class="page_footer">
      <div @click="prePage()">上一页</div>
      <div @click="nextPage()">下一页</div>
    </div>
  </div>
</template>
<script setup>
import Thumbnail from "@/views/list/Thumbnail.vue";
import HomeHeaderNav from "@/components/HomeHeaderNav.vue";
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import {
  selectGoodsPage,
} from "@/network/api";

const router = useRouter();
const routeQuery = useRoute().query;
const avaList = ref([]);

onMounted(() => {
  fillGoodsPage(null);
});

const pager = ({
  total: 0,
  currentPage: routeQuery.currentPage?routeQuery.currentPage:1,
  pageSize: 60,
});
const fillGoodsPage = (locationId) => {
  selectGoodsPage({
    current: pager.currentPage,
    size: pager.pageSize,
    locationId: locationId,
  }).then((res) => {
      if (res?.data?.data?.records) {
        pager.total = res.data.data.total;
        avaList.value = res.data.data.records;
      } else {
        pager.total = 0;
        return;
      }
  });
};
const prePage = () => {
  if(pager.currentPage<=1) {
    return;
  }
  pager.currentPage--;
  selectGoodsPage({
    current: pager.currentPage,
    size: pager.pageSize,
  }).then((res) => {
    if (res?.data?.data?.records) {
      avaList.value = res.data.data.records;
    } else {
      pager.total = 0;
      return;
    }
  });
}

const nextPage = () => {
  if(pager.currentPage*pager.pageSize>=pager.total) {
    return;
  }
  pager.currentPage++;
  selectGoodsPage({
    current: pager.currentPage,
    size: pager.pageSize,
  }).then((res) => {
    if (res?.data?.data?.records) {
      avaList.value = res.data.data.records;
    } else {
      pager.total = 0;
      return;
    }
  });
}

const gotoPreview = (selected) => {
  router.push({
    path: "/preview",
    query: {
      goodsId:selected.id.toString(),
      currentPage:pager.currentPage,
    },
  });
};
</script>

<style lang="scss" scoped>
.home_page {
  position: relative;
  padding-bottom: 36px;
  .page_body {
    padding: 10px 12px;
  }
  .page_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 36px;
    font-size: 12px;
    width: 100%;
    background: #007acc;
    color: #ffffff;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
}
</style>
